package work.konbai.bmc.cms.controller;

import work.konbai.bmc.cms.domain.BillDO;
import work.konbai.bmc.cms.service.BillService;
import work.konbai.bmc.common.annotation.Log;
import work.konbai.bmc.common.constant.enums.ExpenseStateEnum;
import work.konbai.bmc.common.controller.BaseController;
import work.konbai.bmc.common.utils.PageUtils;
import work.konbai.bmc.common.utils.Query;
import work.konbai.bmc.common.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 账单管理
 *
 * @author KonBAI
 * @date 2020-02-01 23:17:08
 */
@Controller
@RequestMapping("/cms/bill")
public class BillController extends BaseController {
	@Autowired
	private BillService billService;

	@GetMapping()
	@RequiresPermissions("cms:bill:bill")
	String Bill(){
		return "cms/bill/bill";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<BillDO> billList = billService.list(query);
		int total = billService.count(query);
		PageUtils pageUtils = new PageUtils(billList, total);
		return pageUtils;
	}


	@ResponseBody
	@GetMapping("/listBill")
	public List<BillDO> billList() {
		BillDO bill = new BillDO();
		bill.setState(ExpenseStateEnum.AGREE.getCode());
		return billService.listBill(bill);
	}

	/**
	 * 添加
	 * @return
	 */
	@GetMapping("/add")
	@RequiresPermissions("cms:bill:add")
	String add(){
	    return "cms/bill/add";
	}

	/**
	 * 编辑
	 * @param id
	 * @param model
	 * @return
	 */
	@GetMapping("/edit/{id}")
	@RequiresPermissions("cms:bill:edit")
	String edit(@PathVariable("id") Long id,Model model){
		BillDO bill = billService.get(id);
		model.addAttribute("bill", bill);
	    return "cms/bill/edit";
	}

	/**
	 * 保存
	 */
	@Log("添加账单")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("cms:bill:add")
	public R save( BillDO bill){
		bill.setCreateUser(getUser().getName());
		if(billService.save(bill)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@Log("修改账单")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("cms:bill:edit")
	public R update( BillDO bill){
		billService.update(bill);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@Log("删除账单")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("cms:bill:remove")
	public R remove( Long id){
		if(billService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}

	/**
	 * 批量删除
	 */
	@Log("批量删除账单")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("cms:bill:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		billService.batchRemove(ids);
		return R.ok();
	}

	/**
	 * 详情
	 */
	@GetMapping("/detail/{id}")
	// @RequiresPermissions("cms:bill:add")
	String detail(@PathVariable("id") Long id, Model model){
		BillDO bill = billService.get(id);
		model.addAttribute("bill", bill);
		return "cms/bill/detail";
	}

}
